package com.goscom.app.deeset;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.goscom.app.database.DBAdapter;

public class MerchandiserName extends Activity implements OnClickListener,
		OnFocusChangeListener {

	private EditText edtTit, edtFore, edtSur, edtGen, edtArea, edtAMName,
			edtTLName, edtDate;
	private TextView txtMerId;
	private String strMerId = "";
	private String strUserId = "";
	private DBAdapter mDB;
	private SharedPreferences sharedPref;
	private DatePickerDialog mDatePicker;
	private boolean mBlnFull;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.merchandisername);

		edtTit = (EditText) findViewById(R.id.edtPerNameTit);
		edtFore = (EditText) findViewById(R.id.edtPerNameFore);
		edtSur = (EditText) findViewById(R.id.edtPerNameSur);
		edtGen = (EditText) findViewById(R.id.edtPerNameGender);
		edtArea = (EditText) findViewById(R.id.edtPerNameArea);
		edtAMName = (EditText) findViewById(R.id.edtPerNameAMName);
		edtTLName = (EditText) findViewById(R.id.edtPerNameTLName);
		edtDate = (EditText) findViewById(R.id.edtPerNameDate);
		txtMerId = (TextView) findViewById(R.id.txtPerNameId);

		sharedPref = getSharedPreferences("DEESET", Context.MODE_PRIVATE);
		strUserId = getStringPref(sharedPref, "userid", "1");

		mDB = new DBAdapter(MerchandiserName.this);
		mDB.open();

		getMerId();

		edtDate.setOnClickListener(this);
		edtDate.setOnFocusChangeListener(this);
	}

	public void getMerId() {
		try {
			strMerId = getIntent().getStringExtra("merid");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (strMerId == null) {
			randomMerId();
			edtDate.setText(new StringBuilder()
					.append(Calendar.getInstance().getTime().getDate())
					.append("/")
					.append(Calendar.getInstance().getTime().getMonth() + 1)
					.append("/")
					.append(Calendar.getInstance().getTime().getYear() + 1900));
		} else {
			ContentValues content = new ContentValues();
			content = mDB.queryDataByMerId("MERNAME", strMerId);
			if (content != null) {
				edtTit.setText(content.getAsString("title"));
				edtFore.setText(content.getAsString("forename"));
				edtSur.setText(content.getAsString("surname"));
				edtGen.setText(content.getAsString("gender"));
				edtArea.setText(content.getAsString("area"));
				edtAMName.setText(content.getAsString("amname"));
				edtTLName.setText(content.getAsString("tlname"));
				edtDate.setText(content.getAsString("date"));
			}
		}
		if (mBlnFull) {
			txtMerId.setText("Account was full merid");
		} else {
			txtMerId.setText(strMerId);
		}

	}

	private void randomMerId() {
		ArrayList<Integer> alstMerIdExist = mDB.queryAllMerIdByUser(strUserId);
		Random random = new Random();
		if (alstMerIdExist.size() > 0) {
			ArrayList<Integer> alstMerId = new ArrayList<Integer>();
			for (int i = 0; i < 10000; i++) {
				alstMerId.add(i);
			}
			for (int i = 0; i < alstMerIdExist.size(); i++) {
				alstMerId.remove(alstMerIdExist.get(i));
			}
			Log.i("Size", alstMerId.size() + "");
			if (alstMerId.size() == 0) {
				Toast.makeText(this, "Account was full merid",
						Toast.LENGTH_LONG).show();
				mBlnFull = true;
			} else {
				int intRand = random.nextInt(alstMerId.size() - 1);
				int intMerId = alstMerId.get(intRand);
				toStringMerId(intMerId);
			}
		} else {
			int intMerId = random.nextInt(9999);
			toStringMerId(intMerId);
		}
	}

	private void toStringMerId(int intMerId) {
		if (intMerId < 10) {
			strMerId = String.valueOf("000" + intMerId);
		} else {
			if (intMerId < 100) {
				strMerId = String.valueOf("00" + intMerId);
			} else {
				if (intMerId < 1000) {
					strMerId = String.valueOf("0" + intMerId);
				} else {
					strMerId = String.valueOf(intMerId);
				}
			}
		}
	}

	public void Logout(View v) {
		if (!mBlnFull) {
			saveDB();
			if (mDB != null) {
				mDB.close();
			}
		}
		startActivity(new Intent(MerchandiserName.this, Login.class));
	}

	public void goNext(View v) {
		if (mBlnFull) {
			Toast.makeText(this, "Account was full merid", Toast.LENGTH_LONG)
					.show();
		} else {
			saveDB();
			if (mDB != null) {
				mDB.close();
			}
			startActivity(new Intent(MerchandiserName.this,
					PersonalImformation.class).putExtra("merid", strMerId));
		}
	}

	public void Save(View v) {
		if (mBlnFull) {
			Toast.makeText(this, "Account was full merid", Toast.LENGTH_LONG)
					.show();
		} else {
			saveDB();
			Toast.makeText(MerchandiserName.this, "Save Successful!",
					Toast.LENGTH_LONG).show();
		}
	}

	public void saveDB() {
		if (mDB.queryMerchandiser("MERNAME", strMerId) == 0) {
			insertDB();
		} else {
			mDB.deleteData("MERNAME", strMerId);
			mDB.deleteData("MER", strMerId);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues cont = new ContentValues();
		cont.put("merid", strMerId);
		cont.put("userid", strUserId);
		mDB.insertData("MER", cont);
		ContentValues content = new ContentValues();
		content.put("merid", strMerId);
		content.put("title", edtTit.getText().toString());
		content.put("forename", edtFore.getText().toString());
		content.put("surname", edtSur.getText().toString());
		content.put("gender", edtGen.getText().toString());
		content.put("area", edtArea.getText().toString());
		content.put("amname", edtAMName.getText().toString());
		content.put("tlname", edtTLName.getText().toString());
		content.put("date", edtDate.getText().toString());
		mDB.insertData("MERNAME", content);
	}

	public String getStringPref(SharedPreferences sharedPref, String name,
			String valrtn) {
		return sharedPref.getString(name, valrtn);
	}

	public void setStringPref(SharedPreferences sharedPref, String name,
			String value) {
		SharedPreferences.Editor editor = sharedPref.edit();
		editor.putString(name, value);
		editor.commit();
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}

	private DatePickerDialog.OnDateSetListener mDateListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {
			edtDate.setText(new StringBuilder().append(selectedDay).append("/")
					.append(selectedMonth + 1).append("/").append(selectedYear));
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.edtPerNameDate:
			mDatePicker = new DatePickerDialog(this, mDateListener, Calendar
					.getInstance().get(Calendar.YEAR), Calendar.getInstance()
					.get(Calendar.MONTH), Calendar.getInstance().get(
					Calendar.DAY_OF_MONTH));
			mDatePicker.show();
			break;
		default:
			break;
		}
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		switch (v.getId()) {
		case R.id.edtPerNameDate:
			if (hasFocus) {
				mDatePicker = new DatePickerDialog(this, mDateListener,
						Calendar.getInstance().get(Calendar.YEAR), Calendar
								.getInstance().get(Calendar.MONTH), Calendar
								.getInstance().get(Calendar.DAY_OF_MONTH));
				mDatePicker.show();
			} else {

			}
			break;
		default:
			break;
		}
	}
}
